package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import custom.Announcement;
import custom.Message;

/**
 * Servlet implementation class addOrRemoveAnnouncementServlet
 */
@WebServlet("/addOrRemoveAnnouncementServlet")
public class addOrRemoveAnnouncementServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addOrRemoveAnnouncementServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aor = request.getParameter("addOrRemove");
		String txt = request.getParameter("newAnnouncement");
		int id = Integer.parseInt(request.getParameter("idToRemove"));

		if(aor.equals("add")){
			Announcement.saveAnnouncement(txt);
		}else{
			Announcement.remove(id);
		}
		
		RequestDispatcher dispatch = request.getRequestDispatcher("editAnnouncements.jsp");
		dispatch.forward(request, response);
	}

}
